package persistency.controller;

import java.util.ArrayList;
import java.util.Collection;

import model.Business;
import model.BusinessTypeEnum;
import model.Course;
import model.CourseHistory;
import persistency.ArgIO;

public class CourseHistoryController  extends BusinessController{

	private static StringBuilder callableStatement;
	
	private static CourseHistoryController instance = new CourseHistoryController();
	private CourseHistoryController(){}
	
	public static CourseHistoryController instance(){
		return instance;
	}
	
	public boolean insertCourseHistory(CourseHistory courseHistory) {
		callableStatement = new StringBuilder("{call sp_insertcoursehistory(?,?,?,?)}");
		return insert(courseHistory,callableStatement,4);
	}
	
	public CourseHistory getCourseHistory(String id){
		return (CourseHistory)getBusinessObject(id,new StringBuilder("{call sp_getbyidcoursehistory(?)}"),BusinessTypeEnum.COURSEHISTORY);
	}

	public Collection<CourseHistory> getByStudentIDCourseHistories(String id) {
		Collection<CourseHistory> col = new ArrayList<CourseHistory>();
		col.addAll((Collection<? extends CourseHistory>) getBusinessObjects(id,new StringBuilder("{call sp_getbystudentidcoursehistory(?)}"),BusinessTypeEnum.COURSEHISTORY,java.sql.Types.CHAR));
		return col;
	}
	
	public Collection<CourseHistory> getAll() {
		Collection<CourseHistory> col = new ArrayList<CourseHistory>();
		col.addAll((Collection<? extends CourseHistory>) getBusinessObjects(new StringBuilder("{call sp_getallcoursehistory}"),BusinessTypeEnum.COURSEHISTORY));
		return col;
	}
	
	

	@Override
	public void prepare(Business business, Object[] args, int[] argsType, ArgIO[] argsIO, int i) throws Exception {
		/*
		 in studentnumber varchar(25),
   	 in coursecode integer,
   	 in schoolyear varchar(25),
   	 in score integer
		 */
		CourseHistory courseHistory = (CourseHistory) business;
		switch (i) {
		case 0: {
			args[i] = courseHistory.getStudent().getStudentNumber();
			argsType[i] = java.sql.Types.CHAR;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 1: {
			args[i] = courseHistory.getCourse().getCode();
			argsType[i] = java.sql.Types.INTEGER;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 2: {
			args[i] = courseHistory.getSchoolYear();
			argsType[i] = java.sql.Types.CHAR;
			argsIO[i] = ArgIO.IN;
			break;
		}
		case 3: {
			args[i] = courseHistory.getScore();
			argsType[i] = java.sql.Types.INTEGER;
			argsIO[i] = ArgIO.IN;
			break;
		}

		default:
			throw new Exception("Error while creating SP createCourse");
		}
		
	}
	
	
	
}
